<?php
session_start();
error_reporting(E_ALL | E_STRICT);
ini_set('display_errors', true);
//Si on veux ce connecter
if(isset($_POST["connexion"]))
{
    //Connetion à la base
    include_once("connect.php");
    $query = "SELECT * FROM ap3_sp_utilisateur WHERE mail='".mysql_real_escape_string ( $_POST["mail"])."' AND MDP='".md5($_POST["MDP"])."' ";
    $result = mysql_query($query);
    $ligne = mysql_num_rows($result);
    //Vérification des champs
    if($ligne == 1)
    {
       $query2 = "select ID from ap3_sp_utilisateur WHERE mail='".mysql_real_escape_string ( $_POST["mail"])."' AND MDP='".md5($_POST["MDP"])."' ";
       $result=mysql_query($query2);
       $row=mysql_fetch_assoc($result);
       $_SESSION["auth"] = $row["ID"];
       $code=rand();
       $_SESSION["ID"] =  $code;
       mysql_query("INSERT INTO ap3_sp_connection 
        VALUES (
        NULL , '".$row['ID']."', '".$code."', CURTIME( ),CURDATE( )
        );
        ");
        
       header("Location:.");           
    }
    else
    {
        //fermer la session
        unset($_SESSION["auth"]);
        header("Location:.");
    }
    
}
//Si on veux se deconnecter
if(isset($_POST["btLogout"]))
{
    unset($_SESSION["auth"]);
    header("Location:.");
    
}
?>